// @charset "utf-8";
/**
 * 强弱评级画图
 */
define(function(require, exports, module) {

	var qrpjChart = {
		fyzztUrl : 'http://www.iwencai.com/stockpick/search?typed=0&preParams=&ts=1&f=1&qs=pc_$client$_stock_stock_all_pcsyzt&source=client&tid=stockpick&w=%E4%BB%8A%E5%A4%A9%E9%9D%9E%E4%B8%80%E5%AD%97%E7%BA%BF%E6%B6%A8%E5%81%9C%E7%9A%84%E8%82%A1%E7%A5%A8',
		ztUrl : 'http://www.iwencai.com/stockpick/search?typed=0&preParams=&ts=1&f=1&qs=pc_$client$_stock_stock_all_pcsyzt&source=client&tid=stockpick&w=%E4%BB%8A%E5%A4%A9%E6%B6%A8%E5%81%9C%E7%9A%84%E8%82%A1%E7%A5%A8',
		dtUrl : 'http://www.iwencai.com/stockpick/search?typed=0&preParams=&ts=1&f=1&qs=pc_$client$_stock_stock_all_pcsyzt&source=client&tid=stockpick&w=%E4%BB%8A%E5%A4%A9%E8%B7%8C%E5%81%9C%E7%9A%84%E8%82%A1%E7%A5%A8',
		changeSize: function () {
			var windowWidth = $(window).width(),
				windowHeight = $(window).height();
			$('.container').height(windowHeight).width(windowWidth);
			$('.tab-content-item').height(windowHeight - 26);
			$('.chart-content, .chart-c').height(windowHeight - 26 - 15)
				.width(windowWidth - 4);

			// $('#view_div_dqfxchart').height(windowHeight - 26 - 5);

			$('.reading-content').height(windowHeight - 30)
				.width(windowWidth - 4);

			var readChartWidth = (windowWidth - 22) * 0.4;
			$('#view_div_readchart').height(readChartWidth * 0.7)
				.width(readChartWidth);

			// xgsg
			$('.xgsg-content').height(windowHeight - 26).width(windowWidth - 10);
			$('.data-box').height(windowHeight - 26 - 28 - 17);
		},
		render: function(result, param) {
			var self = this;

			var chartConfig = {
				xAxis: commonMethod.xAxis,
				tickInterval: 120,
				tickPositions: [0, 120, 241],
				toolTipDate: '',
				ztData: commonMethod.emptyData,
				dtData: commonMethod.emptyData,
				fyzztData: commonMethod.emptyData,
				newZtData: '--',
				newDtData: '--',
				newFyzztData: '--'
			};
			if (!$.isEmptyObject(result)) {
				var ztData = [];
				var dtData = [];
				var fyzztData = [];
				if (param.period === 'min') {
					$.each(result.data, function(i, val) {
						ztData.push(val.zt);
						dtData.push(val.dt);
						fyzztData.push(val.fyzzt);
					});
					chartConfig.newZtData = ztData[ztData.length - 1];
					chartConfig.newDtData = dtData[dtData.length - 1];
					chartConfig.newFyzztData = fyzztData[fyzztData.length - 1];


					ztData = commonMethod.completeData(ztData);
					dtData = commonMethod.completeData(dtData);
					fyzztData = commonMethod.completeData(fyzztData);

					chartConfig.toolTipDate = commonMethod.dateSplit(param.begin).substr(5) + ' ';

				} else {
					var date = [];
					$.each(result, function(j, value) {
						date.push(j.substr(4, 2) + '-' + j.substr(6));
						ztData.push(value.zt);
						dtData.push(value.dt);
						fyzztData.push(value.fyzzt);
					});

					chartConfig.newZtData = ztData[ztData.length - 1],
						chartConfig.newDtData = dtData[dtData.length - 1],
						chartConfig.newFyzztData = fyzztData[fyzztData.length - 1];

					chartConfig.xAxis = date;
					chartConfig.tickInterval = Math.ceil(date.length / 10);
					chartConfig.tickPositions = null;
					toolTipDate = '';
				}
				chartConfig.ztData = ztData;
				chartConfig.dtData = dtData;
				chartConfig.fyzztData = fyzztData;

				self.renderChart(chartConfig);

			} else {
				self.renderChart(chartConfig);
			}

		},
		renderChart: function(chartConfig) {
			var self = this;
			var $chartDom = $('#view_div_qrpjchart');
			var xAxis = chartConfig.xAxis,
				tickInterval = chartConfig.tickInterval,
				tickPositions = chartConfig.tickPositions,
				toolTipDate = chartConfig.toolTipDate,
				ztData = chartConfig.ztData,
				dtData = chartConfig.dtData,
				fyzztData = chartConfig.fyzztData,
				newZtData = chartConfig.newZtData,
				newDtData = chartConfig.newDtData,
				newFyzztData = chartConfig.newFyzztData;

			var yellowColor = '#efefef';
			if (commonMethod.thsSkin == 'white') {
				yellowColor = '#e08900';
			}
			var chartOption = {
				credits: {
					enabled: false
				},
				title: {
					text: ''
				},
				chart: {
					backgroundColor: 'rgba(0,0,0,0)',
					style: {
						fontFamily: 'arial, Microsoft yahei'
					},
					marginTop: 36,
					marginLeft: 40,
					events : {
						click : function () {
							$('.interval-btn').addClass('hide');
						}
					}
				},
				series: [{
					name: '非一字涨停',
					type: 'line',
					color: yellowColor,
					lineWidth: 1,
					marker: {
						enabled: false,
						states: {
							hover: {
								fillColor: '#efefef',
								radius: 2,
								lineColor: 'rgba(200, 200, 200, 1)',
								lineWidthPlus: 1
							}
						}
					},
					data: fyzztData,
					point: {
						events: {
							mouseOver: function() {
								$('.qrpj-legend:first').text(this.y);
							}
						}
					}
				}, {
					name: '涨停',
					type: 'line',
					color: '#ff3232',
					lineWidth: 1,
					marker: {
						enabled: false,
						states: {
							hover: {
								fillColor: '#ff3232',
								radius: 2,
								lineColor: 'rgba(255, 255, 255, 1)',
								lineWidthPlus: 1
							}
						}
					},
					data: ztData,
					point: {
						events: {
							mouseOver: function() {
								$('.qrpj-legend:eq(1)').text(this.y);
							}
						}
					}
				}, {
					name: '跌停',
					type: 'line',
					color: '#00e600',
					lineWidth: 1,
					marker: {
						enabled: false,
						states: {
							hover: {
								fillColor: '#2aaa2a',
								radius: 2,
								lineColor: 'rgba(255, 255, 255, 1)',
								lineWidthPlus: 1
							}
						}
					},
					data: dtData,
					point: {
						events: {
							mouseOver: function(e) {
								$('.qrpj-legend:eq(2)').text(this.y);
							}

						}
					}
				}],
				plotOptions: {
					series: {
						stickyTracking: false,
						events: {
							legendItemClick: function() {
								if (this.name == '非一字涨停') {
									commonMethod.openUrlWeb(self.fyzztUrl, 4);
								} else if (this.name == '涨停') {
									commonMethod.openUrlWeb(self.ztUrl, 4);
								} else if (this.name == '跌停') {
									commonMethod.openUrlWeb(self.dtUrl, 4);
								}
								return false;
							}
						}
					},
					line: {
						states: {
							hover: {
								lineWidthPlus: 0
							}
						}
					}
				},
				xAxis: {
					lineWidth: 1,
					tickLength: 4,
					lineColor: '#808080',
					tickColor: '#808080',
					tickInterval: tickInterval,
					tickPositions: tickPositions,
					title: {
						text: ''
					},
					categories: xAxis,
					gridLineWidth: 0,
					labels: {
						align: 'center',
						rotation: 0,
						style: {
							color: '#afafaf'
						}
					},
					showLastLabel: true
				},
				yAxis: {
					allowDecimals: false,
					lineWidth: 1,
					tickColor: '#808080',
					tickPosition: 'outside',
					gridLineWidth: 0,
					tickLength: 4,
					tickWidth: 1,
					//tickInterval: 500,
					// tickPixelInterval: 40,
					// showLastLabel:true,
					title: {
						text: ''
					},
					lineColor: '#808080',
					labels: {
						style: {
							color: '#afafaf'
						},
						x: -7
					},
					min: 0
				},
				legend: {
					enabled: true,
					align: 'right',
					floating: false,
					verticalAlign: 'top',
					symbolHeight: 20,
					symbolWidth: 14,
					itemStyle: {
						color: '#dedede',
						fontFamily: 'simsun',
						fontWeight: 'normal'
					},
					itemHiddenStyle: {},
					itemHoverStyle: {
						color: '#bbb'
					},
					useHTML: true,
					labelFormatter: function() {
						if (this.name === '涨停') {
							return '<a href="###" title="包括ST股 沪深A股" class="view_a_qrpjlegend textline"><span class="qrpj-legend-item">' + this.name + '<i class="qrpj-legend view_i_qrpjrise">' + newZtData + '</i></span></a>';
						}
						if (this.name === '跌停') {
							return '<span class="qrpj-legend-item textline">' + this.name + '<i class="qrpj-legend view_i_qrpjfall">' + newDtData + '</i></span>';
						}
						if (this.name === '非一字涨停') {
							return '<span class="qrpj-legend-item textline">' + this.name + '<i class="qrpj-legend view_i_qrpjfall">' + newFyzztData + '</i></span>';
						}
					},
					y: -8
				},
				tooltip: {
					shared: true,
					enabled: true,
					style: {
						color: '#acb8aa',
						padding: 2
					},
					hideDelay: 10,
					backgroundColor: '#410080',
					borderWidth: 0,
					crosshairs: [{
						width: 1,
						color: '#afafaf'
					}],
					formatter: function() {
						return toolTipDate + this.x;
					},
					positioner: function(labelWidth, labelHeight, point) {
						var x = point.plotX + 42 - parseInt(labelWidth / 1);
						return {
							x: x,
							y: $('#view_div_qrpjchart').height() - 36
						};
					}
				}
			};
			self.changeSize();
			$chartDom.highcharts(chartOption);
			this.qrpjHighcahrts = $chartDom.highcharts();
		},
		qrpjHighcahrts: null,
		reflowChart: function() {
			if (this.qrpjHighcahrts != null) {
				this.qrpjHighcahrts.reflow();
			}
		}
	};

	module.exports = qrpjChart;
});